package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossUnionShipData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 */
public class PeakSeacraftCrossUnionShipDataDaoImpl extends TempDataObjectDao<PeakSeacraftCrossUnionShipData> {
    public static String getPeakSeacraftCrossUnionShipDataSql = "select * from `t_c_activity_peak_seacraft_union_ship` where `activityId`=? and unionGroup=?";
    public Map<String, PeakSeacraftCrossUnionShipData> getPeakSeacraftCrossUnionShipDataMap(int activityId, int unionGroup) {
        Map<String, PeakSeacraftCrossUnionShipData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getPeakSeacraftCrossUnionShipDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakSeacraftCrossUnionShipData data = parse(rs);
                dataMap.put(data.getUnionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updatePeakSeacraftCrossUnionShipDataSql = "update `t_c_activity_peak_seacraft_union_ship` set totalDurable=?,remainDurable=?,fixRefreshTime=?,speedRecordList=?,distance=?,refreshTime=?,endTime=?,donationExp=? where activityId=? and unionGroup=? and unionId=?";
    @Override
    protected boolean updateData(PeakSeacraftCrossUnionShipData peakSeacraftCrossUnionShipData) {
        peakSeacraftCrossUnionShipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getTotalDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getRemainDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getFixRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionShipData.getSpeedRecordList())));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionShipData.getDonationExp()));

        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionShipData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionShipData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUnionShipData.getUnionId()));
        if (getDb().executeNoneQuery(updatePeakSeacraftCrossUnionShipDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftCrossUnionShipDataSql = "insert into `t_c_activity_peak_seacraft_union_ship`(activityId,unionGroup,unionId,totalDurable,remainDurable,fixRefreshTime,speedRecordList,distance,refreshTime,endTime,donationExp) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftCrossUnionShipData peakSeacraftCrossUnionShipData) {
        peakSeacraftCrossUnionShipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionShipData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionShipData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.VARCHAR, peakSeacraftCrossUnionShipData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getTotalDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getRemainDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getFixRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUnionShipData.getSpeedRecordList())));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUnionShipData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUnionShipData.getDonationExp()));
        if (getDb().executeNoneQuery(insertPeakSeacraftCrossUnionShipDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftCrossUnionShipData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftCrossUnionShipData peakSeacraftCrossUnionShipData = new PeakSeacraftCrossUnionShipData();
        peakSeacraftCrossUnionShipData.setActivityId(rs.getInt("activityId"));
        peakSeacraftCrossUnionShipData.setUnionGroup(rs.getInt("unionGroup"));
        peakSeacraftCrossUnionShipData.setUnionId(rs.getString("unionId"));
        peakSeacraftCrossUnionShipData.setTotalDurable(rs.getLong("totalDurable"));
        peakSeacraftCrossUnionShipData.setRemainDurable(rs.getLong("remainDurable"));
        peakSeacraftCrossUnionShipData.setFixRefreshTime(rs.getLong("fixRefreshTime"));
        peakSeacraftCrossUnionShipData.setSpeedRecordList(JSON.parseObject(rs.getString("speedRecordList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCurSpeedInfo>>() {}));
        peakSeacraftCrossUnionShipData.setDistance(rs.getLong("distance"));
        peakSeacraftCrossUnionShipData.setRefreshTime(rs.getLong("refreshTime"));
        peakSeacraftCrossUnionShipData.setEndTime(rs.getLong("endTime"));
        peakSeacraftCrossUnionShipData.setDonationExp(rs.getInt("donationExp"));

        return peakSeacraftCrossUnionShipData;
    }

}
